package com.tongshu.naive.chat.interfaces;

import com.alibaba.fastjson.JSON;
import com.tongshu.naive.chat.application.InetService;
import com.tongshu.naive.chat.domain.inet.model.ChannelUserInfo;
import com.tongshu.naive.chat.domain.inet.model.ChannelUserReq;
import com.tongshu.naive.chat.domain.inet.model.InetServerInfo;
import com.tongshu.naive.chat.infrastructure.common.EasyResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api")
public class InetController {

    private Logger logger = LoggerFactory.getLogger(InetController.class);

    @Resource
    private InetService inetService;

    @GetMapping("queryNettyServerInfo")
    public EasyResult queryNettyServerInfo() {
        try {
            return EasyResult.buildEasyResultSuccess(new ArrayList<InetServerInfo>(){{
                add(inetService.queryNettyServerInfo());
            }});
        } catch (Exception e) {
            logger.error("查询NettyServer失败。", e);
            return EasyResult.buildEasyResultError(e);
        }
    }

    @PostMapping("queryChannelUserList")
    public EasyResult queryChannelUserList(String json, String page, String limit) {
        try {
            logger.info("查询通信管道用户信息列表开始。req：{}", json);
            ChannelUserReq req = JSON.parseObject(json, ChannelUserReq.class);
            if (null == req) req = new ChannelUserReq();
            req.setPage(page, limit);
            Long count = inetService.queryChannelUserCount(req);
            List<ChannelUserInfo> list = inetService.queryChannelUserList(req);
            logger.info("查询通信管道用户信息列表完成。list：{}", JSON.toJSONString(list));
            return EasyResult.buildEasyResultSuccess(count, list);
        } catch (Exception e) {
            logger.error("查询通信管道用户信息列表失败。req：{}", json, e);
            return EasyResult.buildEasyResultError(e);
        }
    }
}
